import 'package:flutter/material.dart';
import 'package:shenlingwuliu_flutter/utils/http.dart';
import 'package:shenlingwuliu_flutter/utils/toast.dart';
import 'package:shenlingwuliu_flutter/utils/tokenManager.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final TextEditingController _accountController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();

  void _login() async {
    if (_accountController.text.isEmpty) {
      return ToastUtil.showError('请输入账号');
    }

    if (_passwordController.text.isEmpty) {
      return ToastUtil.showError('请输入密码 ');
    }

    var res = await http.post('/driver/login/account', {
      'account': _accountController.text,
      'password': _passwordController.text
    });
    if (res['code'] != 200) return ToastUtil.showError('登陆失败');

    TokenManager().saveToken(res['data']);

    ToastUtil.showSuccess('登陆成功');
    Navigator.pushNamed(context, '/');
  }

  void _test() {
    Navigator.pushNamed(context, '/');
  }

  @override
  void dispose() {
    _accountController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      width: double.infinity,
      padding: const EdgeInsets.only(top: 30, right: 30, left: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const Text(
            '神领快递',
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          Container(
            padding: const EdgeInsets.only(top: 40),
            child: const Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  '账号登陆',
                  style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
                ),
                Text('手机号登陆>',
                    style: TextStyle(
                        color: Colors.red, fontWeight: FontWeight.bold))
              ],
            ),
          ),
          Container(
            padding: const EdgeInsets.only(top: 40),
            child: TextField(
              controller: _accountController,
              decoration: const InputDecoration(
                border: UnderlineInputBorder(),
                hintText: '请输入账号',
              ),
            ),
          ),
          Container(
            padding: const EdgeInsets.only(top: 20, bottom: 30),
            child: TextField(
              controller: _passwordController,
              obscureText: true,
              decoration: InputDecoration(
                suffixIcon: Image.asset(
                  'assets/images/close_eye.png',
                  width: 15,
                ),
                border: const UnderlineInputBorder(),
                hintText: '请输入密码',
              ),
            ),
          ),
          ElevatedButton(
              onPressed: () {
                _login();
              },
              style: const ButtonStyle(
                  // 添加样式
                  backgroundColor:
                      WidgetStatePropertyAll<Color>(Colors.red) // 设置背景颜色为红色
                  ),
              child: const SizedBox(
                width: 200,
                height: 40,
                child: Center(
                  child: Text(
                    '登陆',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              )),
          ElevatedButton(
              onPressed: () {
                _test();
              },
              style: const ButtonStyle(
                  // 添加样式
                  backgroundColor: WidgetStatePropertyAll<Color>(
                      Color.fromARGB(255, 15, 15, 15)) // 设置背景颜色为红色
                  ),
              child: const SizedBox(
                width: 200,
                height: 40,
                child: Center(
                  child: Text(
                    '测试检测token',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ))
        ],
      ),
    ));
  }
}
